www.gusucode.com > 各种VC自绘控件源码程序 > 各种VC自绘控件源码/code/SkinControls(自绘MFC基本控件 )/SkinControls/SkinControls/SkinControls.cpp

    // SkinControls.cpp : 定义 DLL 的初始化例程。
//

#include "stdafx.h"
#include <afxdllx.h>
#include "SkinControls.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

static AFX_EXTENSION_MODULE SkinControlsDLL = { NULL, NULL };

extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
	// 如果使用 lpReserved,请将此移除
	UNREFERENCED_PARAMETER(lpReserved);

	if (dwReason == DLL_PROCESS_ATTACH)
	{
		TRACE0("SkinControls.DLL 正在初始化!\n");
		
		// 扩展 DLL 一次性初始化
		if (!AfxInitExtensionModule(SkinControlsDLL, hInstance))
			return 0;

		// 将此 DLL 插入到资源链中
		// 注意: 如果此扩展 DLL 由
		//  MFC 规则 DLL(如 ActiveX 控件)隐式链接到
		//  而不是由 MFC 应用程序链接到,则需要
		//  将此行从 DllMain 中移除并将其放置在一个
		//  从此扩展 DLL 导出的单独的函数中。使用此扩展 DLL 的
		//  规则 DLL 然后应显式
		//  调用该函数以初始化此扩展 DLL。否则,
		//  CDynLinkLibrary 对象不会附加到
		//  规则 DLL 的资源链,并将导致严重的
		//  问题。

		new CDynLinkLibrary(SkinControlsDLL);

	}
	else if (dwReason == DLL_PROCESS_DETACH)
	{
		TRACE0("SkinControls.DLL 正在终止!\n");

		// 在调用析构函数之前终止该库
		AfxTermExtensionModule(SkinControlsDLL);
	}
	return 1;   // 确定
}

//手光标
extern "C" _SkinControlsExport HCURSOR GetSysHandCursor()
{
	TCHAR		strWinDir[MAX_PATH] = {0};
	HCURSOR		hHandCursor			= NULL;
	hHandCursor = ::LoadCursor(NULL, MAKEINTRESOURCE(32649));

	// Still no cursor handle - load the WinHelp hand cursor
	if( hHandCursor == NULL )
	{
		GetWindowsDirectory(strWinDir, MAX_PATH);
		_tcscat(strWinDir, _T("\\winhlp32.exe"));

		// This retrieves cursor #106 from winhlp32.exe, which is a hand pointer
		HMODULE hModule = ::LoadLibrary(strWinDir);
		DWORD	dwErr = GetLastError();
		if( hModule != NULL )
		{
			HCURSOR	 hTempCur = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
			hHandCursor = (hTempCur != NULL) ? CopyCursor(hTempCur) : NULL;
			FreeLibrary(hModule);
		}
	}
	return hHandCursor;
}

//获取非透明区域
extern "C" _SkinControlsExport void GetImageRgn(CRgn & rgn, CxImage * image)
{
	rgn.CreateRectRgn(0, 0, 0, 0);

	for (DWORD nYPos = 0; nYPos < image->GetHeight(); nYPos++)
	{
		for (DWORD nXPos = 0;nXPos < image->GetWidth(); nXPos++)
		{
			DWORD nXStartPos = nXPos;

			//透明判断
			do
			{
				if (image->IsTransparent(nXPos, image->GetHeight() - nYPos - 1)) break;
			} while ((++nXPos) < image->GetWidth());

			if ((nXPos - nXStartPos) > 0)
			{
				//创建区域
				CRgn RgnUnite;
				RgnUnite.CreateRectRgn(nXStartPos,nYPos,nXPos,nYPos+1);

				//合并区域
				CombineRgn(rgn,RgnUnite,rgn,RGN_OR);
			}
		}
	}
}

//////////////////////////////////////////////////////////////////////////

//extern "C" _SkinControlsExport void RGBtoHSL(BYTE R,BYTE G,BYTE B,float* H,float* S,float* L)
//{
//	BYTE minval = min(R,min(G,B));
//	BYTE maxval = max(R,max(G,B));
//	float mdiff = float(maxval) - float(minval);
//	float msum  = float(maxval) + float(minval);
//
//	*L = msum / 510.0f;
//
//	if (maxval == minval) 
//	{
//		*S = 0.0f;
//		*H = 0.0f; 
//	}   
//	else 
//	{ 
//		float rnorm = (maxval - R) / mdiff;      
//		float gnorm = (maxval - G) / mdiff;
//		float bnorm = (maxval - B) / mdiff;   
//
//		*S = (*L <= 0.5f) ? (mdiff / msum) : (mdiff / (510.0f - msum));
//
//		if(R == maxval) 
//			*H = 60.0f * (6.0f + bnorm - gnorm);
//		if(G == maxval) 
//			*H = 60.0f * (2.0f + rnorm - bnorm);
//		if(B == maxval) 
//			*H = 60.0f * (4.0f + gnorm - rnorm);
//		if (*H > 360.0f) 
//			*H -= 360.0f;
//	}
//}
//
//extern "C" _SkinControlsExport BYTE HueToRGB(float rm1,float rm2,float rh)
//{
//	while(rh > 360.0f)
//		rh -= 360.0f;
//	while(rh < 0.0f)
//		rh += 360.f;
//
//	if(rh < 60.0f)
//		rm1 = rm1 + (rm2 - rm1) * rh / 60.0f;
//	else if(rh < 180.0f)
//		rm1 = rm2;
//	else if(rh < 240.0f)
//		rm1 = rm1 + (rm2 - rm1) * (240.0f - rh) / 60.0f;
//
//	float n = rm1*255;
//	int m = min((int)n,255);
//	m = max(0,m);
//	return (BYTE)m;//(rm1 * 255);
//}
//
//extern "C" _SkinControlsExport COLORREF HSLtoRGB(float H,float S,float L)
//{
//	BYTE r,g,b;
//
//	L = min(1,L);
//	S = min(1,S);
//
//	if(S == 0.0)
//	{
//		r = g = b = (BYTE)(255 * L);
//	} 
//	else 
//	{
//		float rm1, rm2;
//
//		if (L <= 0.5f) 
//			rm2 = L + L * S;
//		else
//			rm2 = L + S - L * S;
//		rm1 = 2.0f * L - rm2;   
//
//		r = HueToRGB(rm1, rm2, H + 120.0f);
//		g = HueToRGB(rm1, rm2, H);
//		b = HueToRGB(rm1, rm2, H - 120.0f);
//	}
//	return RGB(r,g,b);
//}
//
//extern "C" _SkinControlsExport bool AdjustHSL(int degHue, int perSaturation, int perLuminosity, CImage * pImage)
//{
//	if(perSaturation < 0 || perLuminosity < 0)
//		return false;
//
//	if(degHue == 0 && perSaturation == 100 && perLuminosity == 100)
//		return false;	//未作调整,直接返回
//
//	int nWidth = pImage->GetWidth();
//	int nHeight = pImage->GetHeight();
//	int nCrCount = pImage->GetBPP()/8;
//	LPBYTE pBuf = (LPBYTE)pImage->GetBits();
//	int nPitch = pImage->GetPitch();
//	float H,S,L;  
//	LPBYTE pRed=NULL, pGrn=NULL, pBlu=NULL;
//
//	for (int y=0; y<nHeight; y++) 
//	{
//		for (int x=0; x<nWidth; x++) 
//		{
//			pRed = (pBuf + nPitch*y + x*nCrCount);
//			pGrn = (pBuf + nPitch*y + x*nCrCount+1);
//			pBlu = (pBuf + nPitch*y + x*nCrCount+2);
//
//			if (*pRed == 255 && *pGrn == 0 && *pBlu == 255) continue; //过滤RGB(255,0,255) 
//
//			RGBtoHSL(*pRed,*pGrn,*pBlu,&H,&S,&L);      
//
//			H += degHue;   
//			S = (S*perSaturation/100.0f);   
//			L = (L*perLuminosity/100.0f);   
//
//			COLORREF cr = HSLtoRGB(H,S,L); 
//			*pRed = GetRValue(cr);
//			*pGrn = GetGValue(cr);
//			*pBlu = GetBValue(cr);
//		}
//	}
//
//	return true;
//}
//
//extern "C" _SkinControlsExport bool AdjustColor(int degHue, int perSaturation, int perLuminosity, COLORREF* cr)
//{
//	if(perSaturation < 0 || perLuminosity < 0)
//		return false;
//
//	if(degHue == 0 && perSaturation == 100 && perLuminosity == 100)
//		return false;	//未作调整,直接返回
//
//	float H,S,L;  
//	RGBtoHSL(GetRValue(*cr),GetGValue(*cr),GetBValue(*cr),&H,&S,&L);      
//
//	H += degHue;   
//	S = (S*perSaturation/100.0f);   
//	L = (L*perLuminosity/100.0f);   
//
//	*cr =HSLtoRGB(H,S,L); 
//
//	return true;
//}